package com.base.cn.platform.os.service.qa.qatype;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.dao.qa.qatype.QaTypeDao;
import com.base.cn.platform.os.entity.qa.qatype.QaType;
import com.base.cn.platform.os.entity.qa.qatype.QaTypeCondition;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 问答分类 Biz
 * @author LIZHIPENG
 * @since 2018/4/18
 */
@Service
public class QaTypeBiz extends BaseBiz<QaType, QaTypeDao> {

    /**
     * 查询问答分类的数量
     * @param condition 条件
     * @return
     */
    public BigDecimal count(QaTypeCondition condition){
        return this.count(this.getWhereSql(condition));
    }

    /**
     * 分页查询问答分类列表
     * @param page 分页
     * @param condition 查询条件
     * @return PageInfo<QaType>
     */
    public PageInfo<QaType> queryQaTypeListPage(Pagination page, QaTypeCondition condition){
        String whereSql = getWhereSql(condition);
        PageInfo<QaType> qaTypePageInfo = this.findPage(whereSql, page, null);
        return qaTypePageInfo;
    }

    /**
     * 根据ID查询问答分类
     * @param qaTypeId
     * @return
     */
    public QaType findQaTypeById(BigDecimal qaTypeId){
        QaTypeCondition condition = new QaTypeCondition();
        condition.setIds(qaTypeId.toString());
        condition.setCount(1);
        List<QaType> qaTypeList = this.findQaTypeList(condition);
        if(ObjectUtils.isNotEmpty(qaTypeList)){
            return qaTypeList.get(0);
        }
        return null;
    }

    /**
     * 多条件查询问答分类列表
     * @param condition 查询条件
     * @return
     */
    public List<QaType> findQaTypeList(QaTypeCondition condition){
        String whereSql = getWhereSql(condition);
        List<QaType> qaTypeList = this.find(whereSql, condition.getCount(), null);
        return qaTypeList;
    }

    /**
     * 保存或修改问答分类
     * @param qaType
     * @return
     */
    public Map<String, Object> saveOrUpdateQaType(QaType qaType){
        if(ObjectUtils.isNotEmpty(qaType)){
            if(DataUtil.idIsNotNull(qaType.getId())){
                QaType _qaType = this.findById(qaType.getId());
                qaType.setStatus(_qaType.getStatus());
                this.updateById(qaType, null);
            }else {
                qaType.setStatus(1);
                this.save(qaType);
            }
            return ResultUtil.SUCCESS("问答分类信息保存成功", qaType);
        }else {
            return ResultUtil.ERROR("问答分类信息信息为空，保存失败");
        }
    }

    /**
     * 修改问答分类状态
     * @param qaTypeIds 分类ID串
     * @param status 状态：1.正常 2.禁用 3.删除
     * @return Map<String, Object>
     */
    public Map<String, Object> updateQaTypeStatus(String qaTypeIds, Integer status){
        if(StringUtils.isNotEmpty(qaTypeIds) && StringUtils.isNotEmpty(qaTypeIds.trim()) && ObjectUtils.isNotEmpty(status)){
            String msg = "";
            if(status == 1){
                msg = "恢复";
            }else if(status == 2){
                msg = "禁用";
            }else if(status == 3){
                msg = "删除";
            }
            List<QaType> qaTypeList = this.find(" id in ("+qaTypeIds+")",null,null);
            StringBuffer stringBuffer = new StringBuffer();
            if(ObjectUtils.isNotEmpty(qaTypeList)){
                qaTypeList.forEach(v -> {
                    Integer _statust = v.getStatus();
                    if(_statust != status){
                        stringBuffer.append(v.getId()).append(",");
                    }
                });
            }
            qaTypeIds = StringUtils.subHeadTailString(stringBuffer.toString(),",");
            if(StringUtils.isEmpty(qaTypeIds)){
                return ResultUtil.ERROR("请勿重复操作");
            }
            QaType qaType = new QaType();
            qaType.setStatus(status);
            this.updateByWhereSql(qaType, " id in ("+ qaTypeIds +")");
            return ResultUtil.SUCCESS(msg + "成功");
        }
        return ResultUtil.ERROR("系统繁忙，请稍后重试");
    }

    /**
     * 修改问答分类的讲师数量
     * @param newQaTypeId
     * @param oldQaTypeId
     */
    @Transactional(rollbackFor = IllegalArgumentException.class)
    public void updateTeacherNum(BigDecimal newQaTypeId, BigDecimal oldQaTypeId) {
        QaType _qaType = new QaType();
        if(DataUtil.idIsNotNull(newQaTypeId) && !DataUtil.idIsNotNull(oldQaTypeId)){
            QaType qaType = this.findQaTypeById(newQaTypeId);

            _qaType.setId(newQaTypeId);
            _qaType.setTeacherNum(qaType.getTeacherNum() + 1);
            this.updateById(_qaType, null);
        }else if(!DataUtil.idIsNotNull(newQaTypeId) && DataUtil.idIsNotNull(oldQaTypeId)){
            QaType qaType = this.findQaTypeById(oldQaTypeId);
                if(ObjectUtils.isNotEmpty(qaType)){
                    qaType.setId(oldQaTypeId);
                    _qaType.setTeacherNum(qaType.getTeacherNum() - 1);
                    this.updateById(_qaType, null);
                }
        }else if(DataUtil.idIsNotNull(newQaTypeId) && DataUtil.idIsNotNull(oldQaTypeId) && !newQaTypeId.equals(oldQaTypeId)){
            QaType newQaType = this.findQaTypeById(newQaTypeId);
            QaType oldQaType = this.findQaTypeById(oldQaTypeId);

            _qaType.setId(newQaTypeId);
            _qaType.setTeacherNum(newQaType.getTeacherNum() + 1);
            this.updateById(_qaType, null);

            _qaType.setId(oldQaTypeId);
            _qaType.setTeacherNum(oldQaType.getTeacherNum() - 1);
            this.updateById(_qaType, null);
        }

    }
    //=====================================================
    /**
     * 拼接sql语句
     * @param condition 条件
     * @return
     */
    private String getWhereSql(QaTypeCondition condition){
        String whereSql = "status != 3";
        if(ObjectUtils.isNotEmpty(condition)){
            if(StringUtils.isNotEmpty(condition.getIds()) && StringUtils.isNotEmpty(condition.getIds().trim())){
                whereSql += " and id in ("+ condition.getIds().trim() +")";
            }
            if(StringUtils.isNotEmpty(condition.getTypeName()) && StringUtils.isNotEmpty(condition.getTypeName().trim())){
                whereSql += " and typeName like '%"+ condition.getTypeName().trim() +"%'";
            }
            if(ObjectUtils.isNotEmpty(condition.getStatus())){
                whereSql += " and status="+ condition.getStatus();
            }
        }
        whereSql += " order by id desc";
        return whereSql;
    }
}
